﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Chapter07_Lesson01
{
    class Program
    {
        static void Main(string[] args)
        {
            string state = "Hello world 1";
            string state2  = "Hello world 2";
            string state3 = "Hello world 3";
            string state4 = "Hello world 4";

            ThreadPool.QueueUserWorkItem(ThreadProc, state);
            ThreadPool.QueueUserWorkItem(ThreadProc, state2);

            ThreadProc("foregroung testing");

            Console.WriteLine("Main thread does some work, then it sleeps");

            Thread.Sleep(4000);

            ThreadPool.QueueUserWorkItem(ThreadProc, state3);
            ThreadPool.QueueUserWorkItem(ThreadProc, state4);

            Console.WriteLine("It slept for 4000 ms, but it just woke up ;)");

            int workThreads;
            int completetionPortThreads;

            ThreadPool.GetAvailableThreads(out workThreads, out completetionPortThreads);

            Console.WriteLine("The workThreads = {0} and the completetionPortThreads = {1}", workThreads, completetionPortThreads);
        }

        static void ThreadProc(Object stateInfo)
        {
            if (Thread.CurrentThread.IsBackground)
                Console.WriteLine("This thread is background running.");
            else
                Console.WriteLine("This thread is foreground running.");

            string state = (string)stateInfo;
            Console.WriteLine("Hello there, I'm currently on status = " + state);
        }
    }
}
